<template>
  <div class="my-pages">
    <cp-icon name="my-setting" class="my-setting-icon" @click="toSetting"></cp-icon>
    <div class="my-pages-top">
      <div class="my-pages-top-content">
        <div class="my-pages-top-content-left">
          <van-image width="60px" height="60px" fit="cover" radius="4px" :src="userInfo?.avatar" />
          <div class="my-pages-top-content-left-title">
            <div class="my-pages-top-content-left-title-name">{{ userInfo?.name }}</div>
            <div class="my-pages-top-content-left-title-address">
              签名：{{ userInfo?.signature }}
            </div>
          </div>
        </div>
        <!-- <van-icon name="arrow" size="20" /> -->
      </div>
      <!-- <div class="my-pages-tool">
        <van-row align="center">
          <van-col span="4">
            <div class="my-pages-tool-item">16</div>
            <div class="my-pages-tool-item">关注</div>
          </van-col>
          <van-col span="4"
            ><div class="my-pages-tool-item">16</div>
            <div class="my-pages-tool-item">粉丝</div></van-col
          >
          <van-col span="4"
            ><div class="my-pages-tool-item">16</div>
            <div class="my-pages-tool-item">访客</div></van-col
          >
          <van-col span="4" offset="8">
            <cp-icon name="my-setting" class="my-setting-icon"></cp-icon>
          </van-col>
        </van-row>
      </div> -->
    </div>
    <van-tabs v-model:active="active" sticky shrink @change="handleChange">
      <van-tab v-for="item in tabList" :title="item" :key="item" swipeable>
        <template v-if="item === '动态'">
          <div class="my-pages-content" v-if="mypostData.length > 0">
            <template v-for="(item, index) in mypostData" :key="item.post_id">
              <MonentsCard
                v-if="item.image_urls.length > 1"
                :data="item"
                @like-click="triggerClick($event, index)"
                @delete-post="getMyPostData"
              ></MonentsCard>
              <MonentsCardSimple
                v-else
                :data="item"
                @like-click="triggerClick($event, index)"
                @delete-post="getMyPostData"
              ></MonentsCardSimple>
            </template>
          </div>
          <div class="my-pages-content my-pages-failed-content" v-else>
            <cp-icon name="my-failed" class="my-pages-failed-icon"></cp-icon>
          </div>
        </template>
        <template v-else-if="item === '分享'">
          <div class="my-pages-content" v-if="mySharedData.length > 0">
            <template v-for="(item, index) in mySharedData" :key="item.post_id">
              <MonentsCard
                v-if="item.image_urls.length > 1"
                :data="item"
                @like-click="triggerClick2($event, index)"
                @delete-post="getSharePostData"
              ></MonentsCard>
              <MonentsCardSimple
                v-else
                :data="item"
                @like-click="triggerClick2($event, index)"
                @delete-post="getSharePostData"
              ></MonentsCardSimple>
            </template>
          </div>
          <div class="my-pages-content my-pages-failed-content" v-else>
            <cp-icon name="my-failed" class="my-pages-failed-icon"></cp-icon>
          </div>
        </template>
        <template v-else-if="item === '点赞'">
          <div class="my-pages-content" v-if="myLikedData.length > 0">
            <template v-for="(item, index) in myLikedData" :key="item.post_id">
              <MonentsCard
                v-if="item.image_urls.length > 1"
                :data="item"
                @like-click="triggerClick3($event, index)"
                @delete-post="getMyLikedData"
              ></MonentsCard>
              <MonentsCardSimple
                v-else
                :data="item"
                @like-click="triggerClick3($event, index)"
                @delete-post="getMyLikedData"
              ></MonentsCardSimple>
            </template>
          </div>
          <div class="my-pages-content my-pages-failed-content" v-else>
            <cp-icon name="my-failed" class="my-pages-failed-icon"></cp-icon>
          </div>
        </template>
      </van-tab>
    </van-tabs>
  </div>
</template>
<script setup lang="ts">
import { getUserInfo } from '@/services/user'
import { getLikePost, getMyPost, getSharePost } from '@/services/my'
import MonentsCard from '../Monents/components/Monents-card.vue'
import MonentsCardSimple from '../Monents/components/Monents-card-simple.vue'
import { ref } from 'vue'
import { useRouter } from 'vue-router'
const active = ref(0)
const tabList = ['动态', '分享', '点赞']
type userInfoType = {
  avatar: string
  name: string
  signature: string
  user_id: string
}
const userInfo = ref<userInfoType>()
const getUserInfoData = async () => {
  const res = await getUserInfo()
  userInfo.value = res.data as userInfoType
}
getUserInfoData()
type monentsType = {
  author_avatar: string
  author_name: string
  comment_count: number
  content: string
  created_at: string
  image_urls: string[]
  is_liked: boolean
  like_count: number
  post_id: number
  user_id: string
}
const mypostData = ref<monentsType[]>([])
const getMyPostData = async () => {
  const res = await getMyPost()
  mypostData.value = res.data as monentsType[]
}
const triggerClick = (value: boolean, index: number) => {
  if (mypostData.value) {
    mypostData.value[index].is_liked = value
  }
}
getMyPostData()
const mySharedData = ref<monentsType[]>([])
const getSharePostData = async () => {
  const res = await getSharePost()
  mySharedData.value = res.data as monentsType[]
}
const triggerClick2 = (value: boolean, index: number) => {
  if (mySharedData.value) {
    mySharedData.value[index].is_liked = value
  }
}
getSharePostData()
const myLikedData = ref<monentsType[]>([])
const getMyLikedData = async () => {
  const res = await getLikePost()
  myLikedData.value = res.data as monentsType[]
}
getMyLikedData()
const triggerClick3 = (value: boolean, index: number) => {
  if (myLikedData.value) {
    myLikedData.value[index].is_liked = value
  }
}
const handleChange = (index: number) => {
  if (index === 0) {
    getMyPostData()
  } else if (index === 1) {
    getSharePostData()
  } else {
    getMyLikedData()
  }
}
const router = useRouter()
const toSetting = () => {
  router.push('/setting')
}
</script>
<style lang="scss" scoped>
.my-pages {
  width: 100vw;
  height: 100vh;
  background-color: var(--my-bg-primary);
  &-top {
    height: 207px;
    background-color: transparent;
    box-sizing: border-box;
    color: #fff;
    padding: 80px 12px 0px 12px;
    &-content {
      display: flex;
      align-items: center;
      justify-content: space-between;
      margin-bottom: 15px;
      &-left {
        display: flex;
        justify-content: space-between;
        align-items: center;
        &-title {
          margin-left: 12px;
          &-name {
            font-weight: 700;
            font-size: 22px;
          }
          &-address {
            font-size: 14px;
          }
        }
      }
    }
  }
  &-tool {
    &-item {
      text-align: center;
    }
  }
  &-content {
    background-color: var(--bg-tertiary);
    height: calc(100vh - 300px);
    overflow-y: auto;
  }
  &-failed-content {
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: var(--my-bg-primary);
    .my-pages-failed-icon {
      width: 202px;
      height: 131px;
    }
  }
  .my-setting-icon {
    width: 40px;
    height: 30px;
    position: fixed;
    right: 12px;
    top: 10px;
  }
}
:deep(.van-tabs__wrap) {
  border-radius: 10px 10px 0px 0px;
}
</style>
